var objId = getQueryString("objId");
var $;
layui.config({
	base : "js/"
}).use(['form','layer','jquery'],function(){
	var form = layui.form(),
		layer = parent.layer === undefined ? layui.layer : parent.layer,
		laypage = layui.laypage;
		$ = layui.jquery;
		//加载水票信息
    	$.ajax({
			        url: API_URL_HEAD+"waterTicket/detail/"+objId,
			        type: 'get',
			        async: false,
			        contentType: 'application/json; charset=utf-8',
			        dataType: 'json',
			        success: function (data) {
			        	debugger;
			        	var vo = data.data;
			        	$(".ticketName").val(data.data.ticketName);
			        	var ticketType = "";
			        	if(vo.ticketType == 1)
			        	{
			        		ticketType = "普通票";
			        	}else
			        	{
			        		ticketType = "电子票";
			        	}
			        	$(".ticketType").val(ticketType);
			        	$(":radio[name='isSwap'][value='" + vo.isSwap + "']").prop("checked", "checked");
			        	$(".ticketCostPrice").val(data.data.ticketCostPrice);
			        	$(".ticketMarketPrice").val(data.data.ticketMarketPrice);
			        	$(".remark").val(data.data.remark);
			        	form.render(null,'');
			        }
	   });

 	form.on("submit(addUser)",function(data){
 		//是否添加过信息
	 	var dto = {
 			"ticketCostPrice": $(".ticketCostPrice").val(),
 			"ticketMarketPrice": $(".ticketMarketPrice").val(),
 			"isSwap":data.field.isSwap,
 			"remark": $(".remark").val(),
 			"id":objId
 		};
        $.ajax({
	        url: API_URL_HEAD+"waterTicket/update",
	        type: 'post',
	        contentType: "application/json; charset=utf-8",
	        data: JSON.stringify(dto),
	        dataType: 'json',
	        success: function (data) {
	            if(data.code == 0){
	               //刷新父页面
		 			parent.location.reload();
	            }
	        }
    	});
 		return false;
 	})
 	/*
	 *供应商列表
	 */
	function getSupplierList(){
		var dto = {};
	      $.ajax({
	      	    type: "POST",
	      	    dataType: "json",
	      	    url : API_URL_HEAD+"supplier/select",
	      	    contentType: "application/json; charset=utf-8",
	      	    async:true,
	      	    data:JSON.stringify(dto),
	      	  	success: function (data) {  
	      	  		if(data.code == "0"){
		      	  		var jsArr = data.data;
		      	  		var apartAreaObj = $(".supplierName");
		      	  		createSelectOptions(apartAreaObj,jsArr,"supplierName");
		      	  		form.render(null,"supplierName");
	      	  		}
	      	    },
	      	    error: function(data) { 
	      	    	console.log(data);
	      	    	layer.alert("服务器忙，请稍后重试");
	      	    }
	      	});	
	}
	//生成select options
	function createSelectOptions(selObj,dataArray,name,id){
		for(var i in dataArray){
			var every = dataArray[i];
			if(id==undefined){
				$(selObj).append("<option value='"+every["id"]+"'>"+every[name]+"</option>");
			}else{
				if(every["id"]==id){
					$(selObj).append("<option value='"+every["id"]+"'  selected=\"selected\">"+every[name]+"</option>");
				}else{
					$(selObj).append("<option value='"+every["id"]+"'>"+every[name]+"</option>");
				}
			}
			
		}
	}
})
	//获取url中的参数
function getQueryString(name) {
  var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); // 匹配目标参数
  var result = window.location.search.substr(1).match(reg); // 对querystring匹配目标参数
  if (result != null) {
    return decodeURIComponent(result[2]);
  } else {
    return null;
  }
}
//格式化时间
function formatTime(_time){
    var year = _time.getFullYear();
    var month = _time.getMonth()+1<10 ? "0"+(_time.getMonth()+1) : _time.getMonth()+1;
    var day = _time.getDate()<10 ? "0"+_time.getDate() : _time.getDate();
    var hour = _time.getHours()<10 ? "0"+_time.getHours() : _time.getHours();
    var minute = _time.getMinutes()<10 ? "0"+_time.getMinutes() : _time.getMinutes();
    return year+"-"+month+"-"+day+" "+hour+":"+minute;
}
